//
//  LBPhotoCell.m
//  app
//
//  Created by LINAICAI on 2017/5/11.
//  Copyright © 2017年 NAICAI LI. All rights reserved.
//

#import "LBPhotoCell.h"
#import "LBPhoto.h"
#import "UIImage+LBPhotoBrowser.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface LBPhotoCell()<UIScrollViewDelegate>
@end
@implementation LBPhotoCell
- (void)setPhoto:(LBPhoto *)photo{
    
    self.maskLayer.hidden = !(photo.title.length && photo.content.length);
    self.titleLabel.text = photo.title;
    self.contentLabel.text = photo.content;
    [self.contentLabel sizeToFit];
    //如果是本地图片，直接设置图片
    if (photo.image) {
        self.imageView.image = photo.image;
        [self layoutImageView];
    }
    else{
        //如果是网络图片，使用URL来从网络下载图片
        //没有URL的返回
        if (!photo.url ) {
            return;
        }
//        if (!photo.placeholderImage ) {
//            return;
//        }
        [self.activityIndicatorView startAnimating];
        __weak typeof(self) weakSelf = self;
        [self.imageView sd_setImageWithURL:photo.url placeholderImage:photo.placeholderImage options:SDWebImageRetryFailed progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            __weak typeof(self) strongSelf = weakSelf;
            [strongSelf.activityIndicatorView stopAnimating];
            [weakSelf layoutImageView];
        }];
    }
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, self.maskLayer.frame.size.width-30, 25)];
        _titleLabel.font = [UIFont boldSystemFontOfSize:18];
        _titleLabel.textColor = [UIColor whiteColor];
    }
    return _titleLabel;
}
- (UILabel *)contentLabel{
    if (!_contentLabel) {
        CGFloat y = self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height + 13;
        _contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, y , self.maskLayer.frame.size.width - 30, self.maskLayer.frame.size.height - y)];
        _contentLabel.font = [UIFont systemFontOfSize:14];
        _contentLabel.textColor = [UIColor whiteColor];
        _contentLabel.numberOfLines = 0;
        
    }
    return _contentLabel;
}
- (UIImageView *)maskLayer{
    if (!_maskLayer) {
        _maskLayer = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.contentView.bounds.size.height*2/3, self.contentView.bounds.size.width, self.contentView.bounds.size.height/3)];
        _maskLayer.image = [UIImage imageNamed:@"LBPhotoBrowser.bundle/maskLayer.png"];
        _maskLayer.contentMode = UIViewContentModeScaleToFill;
    }
    return _maskLayer;
}
- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:self.contentView.bounds];
        _scrollView.delegate = self;
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            [_scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
        } else {
            // Fallback on earlier versions
        }
    }
    return _scrollView;
}
- (UIActivityIndicatorView *)activityIndicatorView{
    if (!_activityIndicatorView) {
        _activityIndicatorView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        _activityIndicatorView.hidesWhenStopped = YES;
        _activityIndicatorView.center = self.contentView.center;
    }
    return _activityIndicatorView;
}
- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [UIImageView new];
        _imageView.userInteractionEnabled = YES;
        _imageView.clipsToBounds = YES;
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _imageView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.scrollView];
        [self.scrollView addSubview:self.imageView];
        [self.contentView addSubview:self.activityIndicatorView];
        [self.contentView addSubview:self.maskLayer];
        [self.maskLayer addSubview:self.titleLabel];
        [self.maskLayer addSubview:self.contentLabel];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
    }
    return self;
}
- (void)layoutImageView{
    self.scrollView.zoomScale = 1.0;
    self.scrollView.minimumZoomScale = 1.0;
    self.scrollView.maximumZoomScale = 2.0;
    self.scrollView.contentSize = CGSizeZero;
    if (!self.imageView.image) {
        return;
    }
    CGSize imageSize = self.imageView.image.size;
    CGSize size = [self.imageView.image lb_sizeWithScreenWidth];
    CGFloat width = size.width;
    CGFloat height = size.height;
    if (width < imageSize.width) {
        self.scrollView.maximumZoomScale = imageSize.width / width;
    }
    self.imageView.frame = CGRectMake(self.imageView.frame.origin.x, self.imageView.frame.origin.y, width, height);
    self.scrollView.contentSize = self.imageView.frame.size;
    [self scrollViewDidZoom:self.scrollView];
}
- (void)didZoom{
    if (self.scrollView.zoomScale == self.scrollView.maximumZoomScale) {
        [self.scrollView setZoomScale:self.scrollView.minimumZoomScale animated:YES];
    }
    else{
        [self.scrollView setZoomScale:self.scrollView.maximumZoomScale animated:YES];
    }
}
#pragma mark- UIScrollViewDelegate
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return self.imageView;
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    CGSize imageViewSize = self.imageView.frame.size;
    CGFloat width = imageViewSize.width;
    CGFloat height = imageViewSize.height;
    
    CGFloat insetH = (scrollView.frame.size.width - width) * 0.5;
    CGFloat insetV = (scrollView.frame.size.height - height) * 0.5;
    
    if (insetH < 0) {
        insetH = 0;
    }
    if (insetV < 0) {
        insetV = 0;
    }
    scrollView.contentInset = UIEdgeInsetsMake(insetV, insetH, insetV, insetH);
}
@end
